/** 
 * @Title: CountData.java 
 * @Package com.zizo.other.count 
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月15日 下午3:18:40 
 */
package com.zizo.other.count;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.zizo.util.ConnectionPool;

/**
 * @ClassName: CountData
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月15日 下午3:18:40
 * 
 */
public class CountData implements Runnable {
	private Connection conn;
	private String id;

	public CountData(Connection conn, String id) {
		super();
		this.conn = conn;
		this.id = id;
	}

	public void run() {
		count();
	}

	public List<String> getBBSTable() {
		List<String> list = new ArrayList<String>();
		String sql = "SHOW TABLES FROM bitauto LIKE 'bitauto_bbs_%'";
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				list.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public void count() {
		Statement st = null;
		String sql = "SELECT COUNT(DISTINCT bbs_yhid) FROM " + id;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			if (rs.next()) {
				System.out.println(rs.getInt(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Connection conn = ConnectionPool.getConnection("bitauto", "localhost");
		// Connection conn = ConnectionPool.getConnection("xcar", "root", "",
		// "localhost");
		CountData c = new CountData(conn, null);
		List<String> tables = c.getBBSTable();
		System.out.println(tables.size() + "*****");
		ExecutorService f = Executors.newFixedThreadPool(30);
		for (String t : tables) {
			f.execute(new CountData(conn, t));
		}
		f.shutdown();
	}

}
